<?php

//为了防止重复定义，可以在定义一个新的类的时候判断一下这个类是否已经存在：
if (class_exists('content_form') != true) {

    load_extend('form.class.php');

    class content_form {

        public $modelid;

        function __construct($modelid) {
            $this->modelid = $modelid;
        }

        public function content_add() {
            $modelinfo = $this->get_modelinfo();
            $string = cache($this->modelid . '_model_string');
            if ($string === false) {
                $string = '';
                foreach ($modelinfo as $val) {
                    $fieldtype = $val['fieldtype'];
                    $required = !empty($val['datatype']) ? ' datatype="' . $val['datatype'] . '" nullmsg="' . $val['nullmsg'] . '"  errormsg="' . $val['errormsg'] . '"' : '';
                    $red = $val['datatype'] ? '<span class="red">*</span>' : '';
                    if ($fieldtype == 'input' || $fieldtype == 'number') {
                        $string .= $this->tag_start($red . $val['name']) . '<input type="text" class="form-control" ' . $required . '" value="' . $val['defaultvalue'] . '" name="' . $val['field'] . '" placeholder="' . $val['tips'] . '">' . $this->tag_end();
                    } elseif ($fieldtype == 'textarea') {
                        $string .= $this->tag_start($red . $val['name']) . '<textarea name="' . $val['field'] . '" class="form-control" ' . $required . '"  placeholder="' . $val['tips'] . '" >' . $val['defaultvalue'] . '</textarea>' . $this->tag_end();
                    } elseif ($fieldtype == 'select') {
                        $id = 'id="' . $val['field'] . '" name="' . $val['field'] . '" ' . $required;
                        $string .= $this->tag_start($red . $val['name']) . '<span class="select-box">' . form::select($id, '', string2array($val['setting'])) . '</span>' . $this->tag_end();
                    } elseif ($fieldtype == 'radio' || $fieldtype == 'checkbox') {
                        $string .= $this->tag_start($red . $val['name']) . form::$fieldtype($val['field'], $val['defaultvalue'], string2array($val['setting'])) . $this->tag_end();
                    } elseif ($fieldtype == 'datetime') {
                        $string .= $this->tag_start($red . $val['name']) . form::datetime($val['field'], '', $val['setting']) . $this->tag_end();
                    } else {
                        $string .= $this->tag_start($red . $val['name']) . form::$fieldtype($val['field']) . $this->tag_end();
                    }
                }
                cache($this->modelid . '_model_string', $string);
            }
            return $string;
        }

        public function content_edit($data) {
            $modelinfo = $this->get_modelinfo();
            $string = '';
            foreach ($modelinfo as $val) {
                $fieldtype = $val['fieldtype'];
                $required = !empty($val['datatype']) ? ' datatype="' . $val['datatype'] . '" nullmsg="' . $val['nullmsg'] . '"  errormsg="' . $val['errormsg'] . '"' : '';
                $red = $val['datatype'] ? '<span class="red">*</span>' : '';
                if ($fieldtype == 'input' || $fieldtype == 'number') {
                    $string .= $this->tag_start($red . $val['name']) . '<input type="text" class="form-control" value="' . $data[$val['field']] . '" ' . $required . ' name="' . $val['field'] . '" placeholder="' . $val['tips'] . '">' . $this->tag_end();
                } elseif ($fieldtype == 'textarea') {
                    $string .= $this->tag_start($red . $val['name']) . '<textarea name="' . $val['field'] . '" class="form-control" ' . $required . ' placeholder="' . $val['tips'] . '" >' . $data[$val['field']] . '</textarea>' . $this->tag_end();
                } elseif ($fieldtype == 'select') {
                    $id = 'id="' . $val['field'] . '" name="' . $val['field'] . '" ' . $required;
                    $string .= $this->tag_start($red . $val['name']) . '<span class="select-box">' . form::select($id, $data[$val['field']], string2array($val['setting'])) . '</span>' . $this->tag_end();
                } elseif ($fieldtype == 'radio' || $fieldtype == 'checkbox') {
                    $string .= $this->tag_start($red . $val['name']) . form::$fieldtype($val['field'], $data[$val['field']], string2array($val['setting'])) . $this->tag_end();
                } elseif ($fieldtype == 'datetime') {
                    $string .= $this->tag_start($red . $val['name']) . form::datetime($val['field'], $data[$val['field']], $val['setting']) . $this->tag_end();
                } else {
                    $string .= $this->tag_start($red . $val['name']) . form::$fieldtype($val['field'], $data[$val['field']]) . $this->tag_end();
                }
            }
            return $string;
        }

        public function tag_start($tip) {
            return ' <tr><td class="w_100">' . $tip . '：</td><td>';
        }

        public function tag_end() {
            return '</td></tr>';
        }

        public function get_modelinfo() {
            $modelinfo = cache($this->modelid . '_model');
            if ($modelinfo === false) {
                $model = db('model')->where(array('modelid' => $this->modelid))->find();
                if (!$model) {
                    showmsg('模型不存在！', 'stop');
                }
                $modelinfo = db('model_field')->where(array('modelid' => $this->modelid))->order('listorder ASC')->select();
                cache($this->modelid . '_model', $modelinfo);
                cache($this->modelid . '_model_string', null);
            }
            return $modelinfo;
        }

    }

}